package com.example.cd5160866.urlapp.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.support.annotation.ColorInt;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @author cd5160866
 * @date 2018/10/30
 */
public class BubbleView extends FrameLayout {
    /**
     * 圆形气泡
     */
    public static final int STYLE_CIRCLE = 1;

    /**
     * 胶囊形
     */
    public static final int STYLE_PILL = 2;

    /**
     * 点
     */
    public static final int STYLE_DIAN = 3;


    private static final int CONTENT_TEXT_ID = 0x11;
    private static final int CONTENT_IMAGE_ID = 0X12;

    private int mBubbleColor = 0xFFFF675D;

    private CharSequence mText = "";

    private int mTextColor = Color.WHITE;

    private int mStyle = STYLE_CIRCLE;

    public BubbleView(Context context) {
        this(context, null);
    }

    public BubbleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        TextView textView = new TextView(getContext());
        textView.setText(mText);
        textView.setTextColor(mTextColor);
        textView.setTextSize(10f);
        textView.setId(CONTENT_TEXT_ID);
        textView.setSingleLine(true);
        textView.setGravity(Gravity.CENTER);
        LayoutParams params = new LayoutParams(getDefaultSize(), getDefaultSize());
        ImageView imageView = new ImageView(getContext());
        imageView.setId(CONTENT_IMAGE_ID);
        LayoutParams imageParams = new LayoutParams(dip2px(5f), dip2px(5f));
        imageParams.gravity = Gravity.CENTER;
        addView(imageView, imageParams);
        addView(textView, params);
    }

    public void setText(@Nullable CharSequence text) {
        if (TextUtils.isEmpty(text)) {
            text = "";
        }
        this.mText = text;
        setBubbleTextAndBackGround();
    }

    public void setTextColor(@ColorInt int color) {
        this.mTextColor = color;
        setBubbleTextAndBackGround();
    }

    public void setBubbleColor(@ColorInt int color) {
        this.mBubbleColor = color;
        setBubbleTextAndBackGround();
    }

    public int getBubbleStyle() {
        return mStyle;
    }

    private void setBubbleTextAndBackGround() {
        TextView textView = (TextView) findViewById(CONTENT_TEXT_ID);
        int textWidth = getDefaultSize();
        CharSequence text = mText;
        switch (mText.length()) {
            case 1:
            case 2:
                mStyle = STYLE_CIRCLE;
                textView.setVisibility(VISIBLE);
                textView.setBackground(new CircleDrawable(mBubbleColor, getDefaultSize()));
                break;
            case 3:
                mStyle = STYLE_PILL;
                textView.setVisibility(VISIBLE);
                GradientDrawable rectDrawable = new GradientDrawable();
                rectDrawable.setColor(mBubbleColor);
                rectDrawable.setCornerRadius(dip2px(18));
                textView.setBackground(rectDrawable);
                textWidth = dip2px(42);
                break;
            default:
                mStyle = STYLE_DIAN;
                textView.setBackground(null);
                textView.setVisibility(GONE);
                ImageView imageView = (ImageView) findViewById(CONTENT_IMAGE_ID);
                imageView.setImageDrawable(new CircleDrawable(mBubbleColor, dip2px(5f)));
                text = "";
                break;
        }
        textView.setText(text);
        LayoutParams params = (LayoutParams) textView.getLayoutParams();
        params.width = textWidth;
        textView.setLayoutParams(params);
    }

    private int getDefaultSize() {
        return dip2px(18);
    }

    private int dip2px(float dpValue) {
        final float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    private class CircleDrawable extends Drawable {
        private Paint mPaint;

        private int mColor;

        private int mWidth = 0;

        private RectF rectF = new RectF(0, 0, 0, 0);

        /**
         * @param color 颜色
         * @param width 直径
         */
        CircleDrawable(int color, int width) {
            this.mColor = color;
            this.mWidth = width;
            mPaint = new Paint();
            mPaint.setColor(mColor);
            mPaint.setAntiAlias(true);
        }

        @Override
        public void draw(Canvas canvas) {
            try {
                canvas.drawRoundRect(rectF, mWidth, mWidth, mPaint);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void setAlpha(int alpha) {
            mPaint.setAlpha(alpha);
        }

        @Override
        public void setColorFilter(ColorFilter cf) {
            mPaint.setColorFilter(cf);
        }

        @Override
        public int getOpacity() {
            return PixelFormat.TRANSLUCENT;
        }

        @Override
        public int getIntrinsicHeight() {
            return mWidth;
        }

        @Override
        public int getIntrinsicWidth() {
            return mWidth;
        }

        @Override
        public void setBounds(int left, int top, int right, int bottom) {
            super.setBounds(left, top, right, bottom);
            rectF = new RectF(left, top, right, bottom);
        }
    }
}
